// Initializes the options
function betterga_initializeOptions(){
	var pageDocument = document;

	var generalGbox		= pageDocument.getElementById("betterga-gbox-general");
	var reportsGbox		= pageDocument.getElementById("betterga-gbox-reports");

	var cbItem;
	var radioItem;

	var textItem;

	var script;
	var enabled = false;

	var dependents = '';

	var totalScripts = betterga_scripts.length;
	for(var i = 0; i < totalScripts; i++){
		script = betterga_scripts[i].id;
		enabled = betterga_getBooleanPreference('betterga.enabled.'+script, true);
		cbItem = pageDocument.createElement("checkbox");
		cbItem.setAttribute("id", script);
		cbItem.setAttribute("label", betterga_scripts[i].full_name );
		cbItem.setAttribute("checked", enabled);
		if(betterga_scripts[i].conflicts ){
			dependents = 'new Array(';
			for(var j = 0; j < betterga_scripts[i].conflicts.length; j++){
				dependents += "'"+betterga_scripts[i].conflicts[j]+"'";
				if ( j < (betterga_scripts[i].conflicts.length-1) ) {
					dependents += ", ";
				}
				else{
					dependents += ")";
				}
			}
			cbItem.setAttribute("oncommand", "uncheckDependents(this.checked, "+dependents+" );");
		}

		if(betterga_scripts[i].tab == betterga_tabs.GENERAL ){
			generalGbox.appendChild(cbItem);
		}
		else if(betterga_scripts[i].tab == betterga_tabs.REPORTS ){
			reportsGbox.appendChild(cbItem);
		}
	}

	betterga_describescripts();
}

function betterga_saveOptions(){
	betterga_savePrefs( document.getElementById("betterga-gbox-general").childNodes, "checked");
	betterga_savePrefs( document.getElementById("betterga-gbox-reports").childNodes, "checked");

	betterga_setBooleanPreference("betterga.loaded", false);
}

function betterga_savePrefs(cbArray, trueAttrib){
	for(var i = 0; i < cbArray.length; i++){
		if(cbArray[i]){
			if(cbArray[i].getAttribute("id")){
				if(cbArray[i].getAttribute(trueAttrib) == 'true'){
					betterga_setBooleanPreference("betterga.enabled."+cbArray[i].getAttribute("id"), true);
				}
				else{
					betterga_setBooleanPreference("betterga.enabled."+cbArray[i].getAttribute("id"), false);
				}
			}
		}
	}
}

function betterga_describescripts(){
	document.getElementById("script-listing").contentDocument.write('<html><head><style type="text/css">body { font-family:arial; font-size:9pt; background-color:white; } ul { margin:0; padding:0 } li { margin:0 0 10px 0} a { color:blue; border-bottom:solid blue 1px; } </style></head><body id="script-listing-body"><ul>');
	var totalScripts = betterga_scripts.length;
	for(var i = 0; i < totalScripts; i++){
		if( betterga_scripts[i].homepage != ''){
			document.getElementById("script-listing").contentDocument.write('<li><b><a href="'+ betterga_scripts[i].homepage+'" onclick="javascript:window.open(\''+ betterga_scripts[i].homepage+'\');return false;">'+ betterga_scripts[i].full_name + ' v' + betterga_scripts[i].version +'</a></b><br />'+betterga_scripts[i].description+'<br />'+betterga_usLabels.GetStringFromName("script_by")+' '+betterga_scripts[i].author+'</li>');
		}
	}
	document.getElementById("script-listing").contentDocument.write("</ul></body></html>");
	document.getElementById("script-listing").contentDocument.close();
	return true;
}

function uncheckDependents(checked, dep){
	for(var i = 0; i < dep.length; i++){
		if(checked){
			document.getElementById(dep[i]).checked = false;
		}
	}
}

function betterga_initAdvancedPrefs(){
	//document.getElementById("pref-your_domain").value = betterga_getStringPreference("betterga.pref.your_domain");
	//document.getElementById("pref-attach_trigger").value = betterga_getStringPreference("betterga.pref.attach_trigger");
}

function betterga_saveAdvancedPrefs(){
	//betterga_setStringPreference("betterga.pref.your_domain", document.getElementById("pref-your_domain").value);
	//betterga_setStringPreference("betterga.pref.attach_trigger", document.getElementById("pref-attach_trigger").value);
}